import { _decorator, Component, Node, tween, Animation, Vec3 } from 'cc';
import { EventName } from '../manager/AppControll';
import { SoundManager } from '../manager/SoundManger';
import EvtManger from '../tools/EvtManger';
const { ccclass, property } = _decorator;

@ccclass('GiftGole')
export class GiftGold extends Component {
    private mEndPoint: Vec3 = null;
    private mNSpeed: number = 1000;

    protected onLoad(): void {
    }

    InitGiftGold(pos: Vec3) {
        this.mEndPoint = pos;
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.DoMove();
        })
    }

    DoMove() {
        let time = this.mEndPoint.clone().subtract(this.node.position).length() / this.mNSpeed;
        let node = this.node.getChildByName("goldList");

        const dropAnimation = tween()
            .to(time, { position: this.mEndPoint.clone().subtract(this.node.position) })
            .call(() => {
                SoundManager.Instance<SoundManager>().PlayEffect("ui/getGold");
                EvtManger.do(EventName.coninIntoBag);
                EvtManger.do(EventName.goldUpdate);
            });

        node.children.forEach(nd => {
            tween(nd)
                .then(dropAnimation.clone(nd))
                .start();
            this.scheduleOnce(() => { nd.destroy() }
                , time + 0.01);
        })

        this.scheduleOnce(() => {
            this.node.destroy();
        }, time + 0.1)
    }
}


